// pages/home/price/price_list.js
var util = require('../../../utils/util.js');
var app = getApp();
const newquestions=[];
Page({
  data: {
    showtab: 0,  //顶部选项卡索引
    showtabtype: '', //选中类型
    tabnav: {},  //顶部选项卡数据
    testdataall: [],  //所有数据
    testdata1: [], //数据列表
    testdata2: [], //数据列表
    testdata3: [], //数据列表
    testdata4: [], //数据列表
    testdata5: [], //数据列表
    startx: 0,  //开始的位置x
    endx: 0, //结束的位置x
    critical: 100, //触发切换标签的临界值
    marginleft: 0,  //滑动距离
    hiddenmodalput:true,
    tabnav: {
      tabnum: 5,
      tabitem: [
        {
          "id": 1,
          "type": "A",
          "text": "全部",
          "display":"none",
        },
        {
          "id": 2,
          "type": "B",
          "text": "水费",
           "display": "none",
        },
        {
          "id": 3,
          "type": "C",
          "text": "电费",
           "display": "none",
        },
        {
          "id": 4,
          "type": "D",
          "text": "煤气费",
           "display": "none",
        },
        {
          "id": 5,
          "type": "E",
          "text": "热水费",
          "display": "none",
        },
      ]
    },
    checkAll:false,
    totalD:0,
    totalChoose:0,
    ishidden:true,
  },
  onShow: function (options){
    
  },
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: options.apartment_name
    })
    that.setData({
      apartment_id: options.apartment_id
    })
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        that.setData({
          source: res.data.source,
          uid: res.data.user_id
        })
        that.fetchTabData(0);
      }, fail: function () {
        wx.redirectTo({
          url: '/pages/index/index',
        })
      }
    })
  
  },
  fetchData: function (t) {  //生成数据
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    var that=this;
    const newquestions=new Array();
    util.getReq("/app/sdm/findApartmentDaiFuType", {
      source: that.data.source,
      uid: that.data.uid,
      apartment_id: that.data.apartment_id,
      Type: t
    }, function (datas) {
      if (datas.code == 0) {
        wx.hideLoading();
        var dats = datas.data;
        if (dats) {
          that.setData({
            totalD: dats.length,

          })
          for (let i = 0; i < dats.length; i++) {
            newquestions.push({
              "id": i + 1,
              "reading_id": dats[i].reading_id,
              "room_code": dats[i].room_code,
              "should_pay_date": util.format(dats[i].should_pay_date, "yyyy-MM-dd"),
              "reading_type_name": dats[i].reading_type_name,
              "up_reading": dats[i].up_reading,
              "correct_reading": dats[i].correct_reading,
              "reading_difference": dats[i].reading_difference,
              "total_money":dats[i].total_money,
              "this_reading": dats[i].this_reading,
              "checked": false,
              'order_id': dats[i].order_id
            })
          }
          
          if (dats.length<=0){
            that.setData({
              ishidden: false
            })
          }else{
            that.setData({
              ishidden: true

            })
          }
          that.setData({
            testdata1: newquestions
          })
         
        }
      } else {
        var errMsg = '服务器异常';
        if (datas.desc) {
          errMsg = datas.desc;
        }
        wx.showToast({
          title: errMsg,
        })
     
      }
    })
  
    //return newquestions
  },
  fetchTabData: function (i) {
  
    switch (Number(i)) {
      case 0:
        this.fetchData('A');
       
        break;
      case 1:
        this.fetchData('B')
        
        break;
      case 2:
        this.fetchData('C');
        break;
      case 3:
        this.fetchData('D')
        break;
      case 4:
        this.fetchData('E')
        break;
      default:
        return;
    }
  },
  setTab: function (e) { //设置选项卡选中索引
    const edata = e.currentTarget.dataset;
    this.setData({
      showtab: Number(edata.tabindex),
      showtabtype: edata.type,
      checkAll: false,
      totalD: 0,
      totalChoose: 0,
    })
    this.fetchTabData(edata.tabindex);
  },
/**
 * 全选
 */
  selectalltap: function (e) {    //
    var value = e.detail.value;
    var that=this;
    var showtab = that.data.showtab;
    var checkAll = false;
    if (value && value[0]) {
      checkAll = true;
      let checkboxItems = that.checkGetList(showtab, that); 
      for (var i = 0; i < checkboxItems.length; ++i) {
            checkboxItems[i].checked = true;
          
        }
        that.checkOneDetail(checkboxItems, showtab, that);
        that.setData({
          totalChoose: checkboxItems.length,
      })
    }else{
      let checkboxItems = that.checkGetList(showtab, that);
      for (var i = 0; i < checkboxItems.length; ++i) {
        checkboxItems[i].checked = false;

      }
      that.checkOneDetail(checkboxItems, showtab, that);
      that.setData({
        totalChoose: 0,
      })
    }
    that.setData({
      'checkAll': checkAll,
    });

  },
/**
 * 单选
 */
  checkOne:function(e){
    var that=this;
    let checkboxItems;
    var values = e.detail.value;
    var showtab=that.data.showtab;
    checkboxItems = that.checkGetList(showtab,that); 
      for (var i = 0; i < checkboxItems.length; ++i) {
         checkboxItems[i].checked = false; 
         for (var j = 0; j < values.length; ++j) {
           if (checkboxItems[i].reading_id == values[j]) { 
              checkboxItems[i].checked = true;
               break;
                }
             } 
        that.checkOneDetail(checkboxItems, showtab,that);
       }
    that.setData({
      totalChoose: values.length,
    })
  },
  checkOneDetail: function (checkboxItems, showtab,that){
    that.setData({
      testdata1: checkboxItems
    })
  },
  checkGetList: function (showtab,that){
    
      return that.data.testdata1;
   
  },
  submitD:function(e){
    this.setData({
      hiddenmodalput:false
    })
    
  },
  getcheckBoxIstrue: function (checkboxItems){
    var newCheckArray=[];
    for (let i = 0; i < checkboxItems.length;i++){
      if (checkboxItems[i].checked){
            newCheckArray.push({
              "reading_id": checkboxItems[i].reading_id,
              "room_code": checkboxItems[i].room_code,
              "reading_type_name": checkboxItems[i].reading_type_name,
              "up_reading": checkboxItems[i].up_reading,
              "correct_reading": checkboxItems[i].correct_reading,
              "reading_difference": checkboxItems[i].reading_difference,
              "total_money": checkboxItems[i].total_money,
              "this_reading": checkboxItems[i].this_reading,
              "checked": checkboxItems[i].cheched,
              "order_id": checkboxItems[i].order_id
            })
          }
      }
   
    return newCheckArray;
  },
  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh(true);
  },
  /**
   * 面板点击选中
   */
  panelCheckOne:function(e){
    var that=this;
    let vale = e.currentTarget.dataset.value;
    var showtab = that.data.showtab;
    let checkboxItems;
    checkboxItems = that.checkGetList(showtab, that);
    for (var i = 0; i < checkboxItems.length; ++i) {
      if (checkboxItems[i].reading_id == vale) {
        if (checkboxItems[i].checked == true){
            checkboxItems[i].checked = false;
            that.setData({
              totalChoose: that.data.totalChoose-1,
            })
          }else{
            checkboxItems[i].checked = true;
            that.setData({
              totalChoose: that.data.totalChoose + 1,
            })
          }
          break;
      }
     
    }
    that.checkOneDetail(checkboxItems, showtab, that);
  },
  submitData:function(type){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定保存吗?',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '保存中请稍后...',
            mask: true,
          })
          var showtab = that.data.showtab;
          let checkboxItems = that.checkGetList(showtab, that);
          const newquestions = that.getcheckBoxIstrue(checkboxItems);
          util.getReq("/app/sdm/updateApartmentDaiFuType", {
            source: that.data.source,
            uid: that.data.uid,
            type:type,
            newquestions: JSON.stringify(newquestions),
          }, function (datas) {
            if (datas.code == 0) {
              wx.hideLoading();
              wx.redirectTo({
                url: '/pages/home/zhangdan/zhandan_list',
              })

            } else {

              var errMsg = '服务器异常';
              if (datas.desc) {
                errMsg = datas.desc;
              }
              wx.showToast({
                title: errMsg,
                icon:'none'
              })

            }
          })
        }
      }
    })
  },
  cancelM:function(e){
    this.setData({
      hiddenmodalput: true
    })
    this.submitData(1);
    
  },
  confirmM:function(e){
    this.setData({
      hiddenmodalput: true
    })
    this.submitData(2);
   
  }
})
